﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Test;
using PdfLib;

namespace PdfLib.Test.PdfParser
{
    [ManualTestCase("Path reader", "PDF parser")]
    public partial class TestCase_PathReader : UserControl
    {
        public TestCase_PathReader()
        {
            InitializeComponent();
        }

        private void btn_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Filter = "PDF files(*.pdf)|*.pdf|All files(*.*)|*.*";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                this.txt_PdfPath.Text = dlg.FileName;
                int pageCount = PdfPropertyReader.GetPageCount(this.txt_PdfPath.Text);
                this.num_Page.Value = 1;
                this.num_Page.Maximum = pageCount;

                this.OpenPage(this.txt_PdfPath.Text, (int)this.num_Page.Value);
            }
        }

        private void btn_Open2_Click(object sender, EventArgs e)
        {
            this.OpenPage(this.txt_PdfPath.Text, (int)this.num_Page.Value);
        }

        private void OpenPage(string pdfFilePath, int pageNum)
        {
            try
            {
                List<PdfDrawingInstruction> pdfPaths = 
                    PdfDrawingInstructionReader.GetDrawingInstructionsInPage(
                        pdfFilePath, pageNum);
                this.dv_Paths.Rows.Clear();
                if (pdfPaths != null && pdfPaths.Count > 0)
                {
                    foreach (PdfDrawingInstruction pdfPath in pdfPaths)
                    {
                        DataGridViewRow dr = new DataGridViewRow();
                        DataGridViewTextBoxCell cellPath = new DataGridViewTextBoxCell();
                        cellPath.Value = pdfPath.Instruction;
                        dr.Cells.Add(cellPath);
                        DataGridViewTextBoxCell cellType = new DataGridViewTextBoxCell();
                        cellType.Value = pdfPath.DrawingType.ToString();
                        dr.Cells.Add(cellType);
                        DataGridViewTextBoxCell cellFrom = new DataGridViewTextBoxCell();
                        cellFrom.Value = string.Format("{0},{1}", pdfPath.FromPos.X, pdfPath.FromPos.Y);
                        dr.Cells.Add(cellFrom);
                        DataGridViewTextBoxCell cellTo = new DataGridViewTextBoxCell();
                        cellTo.Value = string.Format("{0},{1}", pdfPath.ToPos.X, pdfPath.ToPos.Y);
                        dr.Cells.Add(cellTo);
                        this.dv_Paths.Rows.Add(dr);
                    }
                }

                SizeF pageSize = PdfPropertyReader.GetPageSize(pdfFilePath, pageNum);

                List<VerticalLine> vLines = PdfDrawingInstructionReader.GetVLines(pdfFilePath, pageNum);
                Bitmap img1=new Bitmap((int)pageSize.Width, (int)pageSize.Height);
                Graphics g1 = Graphics.FromImage(img1);
                if(vLines !=null && vLines.Count>0)
                {
                    foreach(VerticalLine vLine in vLines)
                    {
                        g1.DrawLine(new Pen(Color.Black, 2), vLine.X, vLine.Y1, vLine.X, vLine.Y2);
                    }
                }
                this.pic_VLines.Image = img1;
                g1.Dispose();

                List<HorizontalLine> hLines = PdfDrawingInstructionReader.GetHLines(pdfFilePath, pageNum);
                Bitmap img2=new Bitmap((int)pageSize.Width, (int)pageSize.Height);
                Graphics g2 = Graphics.FromImage(img2);
                if(hLines !=null && hLines.Count>0)
                {
                    foreach(HorizontalLine hLine in hLines)
                    {
                        g2.DrawLine(new Pen(Color.Black, 2), hLine.X1, hLine.Y, hLine.X2, hLine.Y);
                    }
                }
                this.pic_HLines.Image = img2;
                g2.Dispose();

                List<RectangleF> rects = PdfDrawingInstructionReader.GeRectangles(pdfFilePath, pageNum);
                Bitmap img = new Bitmap((int)pageSize.Width, (int)pageSize.Height);
                Graphics g = Graphics.FromImage(img);
                if (rects != null && rects.Count > 0)
                {
                    foreach (RectangleF rect in rects)
                    {
                        g.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2), (int)rect.X, (int)rect.Y, (int)rect.Width, (int)rect.Height);
                    }
                }
                this.pic_Rects.Image = img;
                g.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
